package com.daafan.minilib.activity;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.daafan.minilib.R;

public class WebViewActivity extends BaseActivity {

	private static final String	TAG					= "WebViewActivity";
	public static final String	URL					= "url";
	public static final String	TITLE				= "TITLE";

	private String				orinalURL;

	private FrameLayout			mFullscreenContainer;
	private FrameLayout			mContentView;
	private View				mCustomView			= null;
	private WebView				mWebView;
	private ProgressBar			progressBar;

	private WebViewClient		mWebViewClient		= new MyWebViewClient();
	private WebChromeClient		mWebChromeClient	= new MyWebChromeClient();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_web_view);

		initTopBar("网页", R.drawable.back, 0);

		initViews();
		initWebView();

		String url = getIntent().getStringExtra(URL);
		String title = getIntent().getStringExtra(TITLE);
		if (url != null && !url.equals("")) {
			Log.d(TAG, url);

			// url = "http://youku.com";
			// url = "http://data.iego.net:88/m2/coursedetail.aspx?code=88888888&lesson_id=532beb3b4fd706004435a1a8";
			// url = "http://data.iego.net:88/m2/course.aspx";
			System.out.println("mWebView.loadUrl(url);   " + url);
			mWebView.loadUrl(url);
		}
		if (title != null && !title.equals("")) {
			Log.d(TAG, title);

			setTitile(title);
		}

		// checkOrientation();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			System.out.println("onConfigurationChanged 横屏");
			findViewById(R.id.topbar).setVisibility(View.GONE);

		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			System.out.println("onConfigurationChanged 竖屏");
			findViewById(R.id.topbar).setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onBackPressed() {
		onTopLeftClick();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initViews() {
		mFullscreenContainer = (FrameLayout) findViewById(R.id.fullscreen_custom_content);
		mContentView = (FrameLayout) findViewById(R.id.main_content);
		progressBar = (ProgressBar) findViewById(R.id.progressBar_webView);
		progressBar.setProgress(0);
	}

	@SuppressLint("SetJavaScriptEnabled")
	private void initWebView() {
		mWebView = (WebView) findViewById(R.id.webView1);

		WebSettings settings = mWebView.getSettings();
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);

		// setting.setSupportZoom(false);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		mWebView.setVerticalScrollBarEnabled(false);

		// 视频
		settings.setJavaScriptEnabled(true);
		settings.setJavaScriptCanOpenWindowsAutomatically(true);
		settings.setPluginState(PluginState.ON);
		// if (getPhoneAndroidSDK() < 14)
		// settings.setPluginsEnabled(true);

		settings.setAllowFileAccess(true);
		settings.setLoadWithOverviewMode(true);

		mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		mWebView.setWebViewClient(mWebViewClient);
		mWebView.setWebChromeClient(mWebChromeClient);

		mWebView.setVisibility(View.VISIBLE);
	}

	private void setTitile(String title) {
		initTopBar(title == null ? "标题" : title, R.drawable.back, 0);
	}

	@Override
	protected void onTopLeftClick() {
		if (mWebView.canGoBack())
			mWebView.goBack();
		else
			back();
	}

	private class MyWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);

			System.out.println("shouldOverrideUrlLoading   " + url);
			return true;
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			System.out.println("shouldOverrideUrlLoading   " + url);
			progressBar.setVisibility(View.VISIBLE);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			progressBar.setVisibility(View.GONE);
		}
	}

	private class MyWebChromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			progressBar.setProgress(newProgress + 10);
		}
	}

	public static int getPhoneAndroidSDK() {
		int version = 0;
		try {
			version = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return version;

	}
}
